package com.umpay.config;

import java.lang.reflect.Method;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;

import com.google.gson.Gson;

public class AsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
	private static final Logger log = LogManager.getLogger(AsyncExceptionHandler.class);
	/**
	 * 在无返回值的异步调用中，异步处理抛出异常，AsyncExceptionHandler的handleUncaughtException()会捕获指定异常，原有任务还会继续运行，直到结束。
     * 在有返回值的异步调用中，异步处理抛出异常，会直接抛出异常，异步任务结束，原有处理结束执行。
	 */
	@Override
	public void handleUncaughtException(Throwable ex, Method method, Object... params) {
		// TODO Auto-generated method stub
		Gson gson = new Gson();
		log.info("Async method: {} has uncaught exception,params:{}", method.getName(), gson.toJson(params));
		 log.info("Exception :"+ex.getMessage());
        if (ex instanceof AsyncException) {
            AsyncException asyncException = (AsyncException) ex;
            log.info("asyncException:{}",asyncException.getErrMsg());
        }

        log.info("Exception :");
        ex.printStackTrace();
	}

}
